package com.gxuwz.controller;

import com.gxuwz.entity.User;
import com.gxuwz.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @Description:辅导员个人信息设置模块
 */

@Controller
@RequestMapping("/user")
public class UserInfoController {

    @Autowired
    private UserService userService;

    /**
     * @Description:根据session，查询相应的辅导员个人信息，并跳转至个人信息设置页面
     * @Param:  [javax.servlet.http.HttpServletRequest, org.springframework.web.servlet.ModelAndView]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @GetMapping("/info")
    public ModelAndView getUserInfo(HttpServletRequest request,ModelAndView modelAndView){
        HttpSession session = request.getSession();
        String userId = (String) session.getAttribute("username");
        User userInfo = userService.queryUserById(userId);
        modelAndView.setViewName("personalSettings");
        modelAndView.addObject("userInfo",userInfo);
        return modelAndView;
    }

    /**
     * @Description:实现修改辅导员个人信息功能
     * @Param:  [org.springframework.web.servlet.ModelAndView, com.gxuwz.entity.User, javax.servlet.http.HttpServletRequest]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @RequestMapping("/updateUserInfo")
    public ModelAndView updatePersonal(ModelAndView modelAndView,User user,HttpServletRequest request){
        HttpSession session = request.getSession();
        String userId = (String) session.getAttribute("username");
        user.setUserId(userId);
        boolean flag = userService.updateUserInfo(user);
        if(flag){
            modelAndView.setViewName("login");
            session.removeAttribute("username");
            return modelAndView;
        } else {
            modelAndView.setViewName("personalSettings");
            modelAndView.addObject("message","修改失败");
            return modelAndView;
        }
    }
}

